package com.benin.sms.network

import androidx.lifecycle.ViewModel
import com.benin.sms.model.ToBase
import com.benin.sms.model.ToDoLoginTo
import com.benin.sms.helpipetrpide.UserStateHelp
import kotlinx.coroutines.Dispatchers

object RepoLaunch : RepoBase() {

    fun getAppVersion(caller: ViewModel?, toBase: ToBase) =
        fire(Dispatchers.IO, caller, toBase.showLoading) {
            val response = NetLaunchImp.getAppVersion(toBase)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun doLogin(caller: ViewModel?, doLoginReq: ToDoLoginTo) =
        fire(Dispatchers.IO, caller) {
            val response = NetLaunchImp.doLogin(doLoginReq)
            if (response.code.equals("0000", true)) {
                val obj = response.obj
                UserStateHelp.saveToken(obj.token)
                UserStateHelp.saveMobile(doLoginReq.loginName)
                UserStateHelp.saveLoginName(doLoginReq.loginName)
                UserStateHelp.saveUserId(obj.userId)
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }
}